package nl.infodation.emr.pricingservice.dao.impl;

import java.util.Hashtable;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import nl.infodation.emr.pricingservice.dao.SystemValueRefDAO;
import nl.infodation.emr.pricingservice.entity.SystemDetail;
import nl.infodation.emr.pricingservice.entity.SystemValueRef;

import org.hibernate.QueryException;

public class SystemValueRefDAOImpl extends DAOImpl<SystemValueRef, Integer>
		implements SystemValueRefDAO {	

	private static SystemValueRefDAO instance;
	
	private SystemValueRefDAOImpl() { 
		super();
	}
	
	public static synchronized SystemValueRefDAO getInstance() {
		if (instance == null) {
			instance = new SystemValueRefDAOImpl();
		}
		return instance;
	}	
	
	/* (non-Javadoc)
	 * @see nl.infodation.emr.pricingservice.dao.SystemValueRefDAO#findByRoleAndLangAndSys(java.lang.String, java.lang.Integer, java.lang.String)
	 */
	@SuppressWarnings("unchecked")
	@Override
	public Hashtable<String,List<SystemDetail>> findByRoleAndLangAndSys(String roleName, Integer langId, String...sysNames) {
		Hashtable<String,List<SystemDetail>> result = new Hashtable<>();

		EntityManager entityManager = null;
		try {
			entityManager = createEntityManager(EM_FACTORY);
			for (String sysName : sysNames) {
				Query query = entityManager.createNamedQuery(SystemDetail.QUERY_NAME);
				query.setParameter(1, roleName);
				query.setParameter(2, langId);
				query.setParameter(3, sysName);
				result.put(sysName, query.getResultList());
			}
		} catch (IllegalArgumentException | QueryException ex) {
			LOG.error("Error reading findByRoleAndLang.", ex);
		} finally {
			close(entityManager);
		}
				
		return result;
	}

}
